import pygame
import random

# 设置
width, height = 800, 600
cell_size = 40
num_mines = 40

# 颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gray = (200, 200, 200)
dark_gray = (128, 128, 128)

# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("扫雷")

# 字体
font = pygame.font.Font(None, 36)

# 地图大小
rows = height // cell_size
cols = width // cell_size


# 创建地图
def create_map():
    grid = [[0 for _ in range(cols)] for _ in range(rows)]
    mines = random.sample(range(rows * cols), num_mines)
    for mine in mines:
        r, c = divmod(mine, cols)
        grid[r][c] = -1
        for dr in [-1, 0, 1]:
            for dc in [-1, 0, 1]:
                nr, nc = r + dr, c + dc
                if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] != -1:
                    grid[nr][nc] += 1
    return grid


# 画网格
def draw_grid():
    for r in range(rows):
        for c in range(cols):
            rect = pygame.Rect(c * cell_size, r * cell_size, cell_size, cell_size)
            pygame.draw.rect(screen, gray, rect, 1)


# 画单元格
def draw_cell(r, c, color, text=None):
    rect = pygame.Rect(c * cell_size + 1, r * cell_size + 1, cell_size - 1, cell_size - 1)
    pygame.draw.rect(screen, color, rect)
    if text:
        text_surface = font.render(text, True, black)
        text_rect = text_surface.get_rect(center=rect.center)
        screen.blit(text_surface, text_rect)


# 揭示单元格
def reveal_cell(grid, revealed, r, c):
    if not revealed[r][c]:
        revealed[r][c] = True
        if grid[r][c] == -1:
            draw_cell(r, c, red, "*")
        else:
            draw_cell(r, c, white, str(grid[r][c]) if grid[r][c] > 0 else "")


# 检查游戏是否结束
def check_game_over(grid, revealed):
    for r in range(rows):
        for c in range(cols):
            if revealed[r][c] and grid[r][c] == -1:
                return True
    return False


# 检查游戏是否胜利
def check_victory(grid, revealed):
    for r in range(rows):
        for c in range(cols):
            if not revealed[r][c] and grid[r][c] != -1:
                return False
    return True

def reveal_cell_recursive(grid, revealed, r, c):
    if not revealed[r][c]:
        revealed[r][c] = True
        if grid[r][c] == -1:
            draw_cell(r, c, red, "*")
        else:
            draw_cell(r, c, white, str(grid[r][c]) if grid[r][c] > 0 else "")
            if grid[r][c] == 0:
                for dr in [-1, 0, 1]:
                    for dc in [-1, 0, 1]:
                        nr, nc = r + dr, c + dc
                        if 0 <= nr < rows and 0 <= nc < cols:
                            reveal_cell_recursive(grid, revealed, nr, nc)


# 主循环
def main():
    grid = create_map()
    revealed = [[False for _ in range(cols)] for _ in range(rows)]
    game_over = False
    victory = False
    screen.fill(white)
    draw_grid()
    while not game_over and not victory:
        if game_over:
            print("游戏结束！")
            pygame.time.delay(3000)  # 延时3秒
        if victory:
            print("恭喜你，获胜了！")
            pygame.time.delay(3000)  # 延时3秒


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
                break
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # 鼠标左键
                    r, c = event.pos[1] // cell_size, event.pos[0] // cell_size
                    reveal_cell_recursive(grid, revealed, r, c)
                    game_over = check_game_over(grid, revealed)
                    victory = check_victory(grid, revealed)

        for r in range(rows):
            for c in range(cols):
                if revealed[r][c]:
                    reveal_cell(grid, revealed, r, c)

        pygame.display.flip()

    pygame.quit()

if __name__ == "__main__":
    main()
